// pages/mine/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    tx:'',
    ischange:'false',
    direction:"",
    collection:[],
    work:[],
    like:[],
    focus:[
    ],
    fans:[
    ]
  },
  // 跳转页面
  toNumList(event){
    let type = event.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/fans/index?data='+JSON.stringify({name:this.data.name,type}),
    })
  },
  // 将emoji变成字符
  EmojiExchangeString(str) { 
    var patt=/[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则 
    str = str.replace(patt, function(char){ 
        var H, L, code; 
        if (char.length===2) { 
            H = char.charCodeAt(0); // 取出高位 
            L = char.charCodeAt(1); // 取出低位 
            code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法 
            return "&#" + code + ";"; 
        } else { 
            return char; 
        } 
    }); 
    return str; 
},
  // 变成emoji
  StringExchangeEmoji(str){
    var reg = /\&#.*?;/g;
    var result = str.replace(reg,function(char){
        var H,L,code;
        if(char.length == 9 ){
            code = parseInt(char.match(/[0-9]+/g));
            H = Math.floor((code-0x10000) / 0x400)+0xD800;
            L = (code - 0x10000) % 0x400 + 0xDC00;
            return unescape("%u"+H.toString(16)+"%u"+L.toString(16));
        }else{
            return char;
        }
    });
    return result;
},
  bindKeyInput(event){
    this.setData({
      direction:event.detail.value.trim(),
      ischange:true
    })
  },
  // 修改用户的描述
  updateDir(){
    if(this.data.ischange&&this.data.direction){
      wx.request({
        url: 'http://localhost:8888/changeuser',
        data:{username:this.EmojiExchangeString(this.data.name),direction:this.data.direction},
        method:'POST',
        success:(res)=>{
          if(res.data.code == 200){
            wx.showToast({
              title: '设置成功',
              icon: 'success',
              duration: 2000
            })
          }
        }
      })
    }
  },
  // 获取列表数据
  throttle(fn,wait){
    var timer = null;
    return function(){
        var context = this;
        var args = arguments;
        if(!timer){
            timer = setTimeout(function(){
                fn.apply(context,args);
                timer = null;
            },wait)
        }
    }
},
  handle(name,title){
    this.throttle(()=>{this.getList(name)},1000)
  },
  getList(event){
    let name = event.detail.name
    console.log(name)
    var time = 0;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: time
    })
    wx.request({
      url: `http://localhost:8888/get${name}`,
      data:{name:this.EmojiExchangeString(this.data.name)},
      success:(res)=>{
        time = 2000
        console.log(res.data)
        let list = res.data.list;
        let data = res.data.data?res.data.data:[];
        data = data.map((item,index)=>{
          item.watch = item.check
          item.img = JSON.parse(item.img)[0].url
          item.tx = JSON.parse(item.userid).tx
          item.name = this.StringExchangeEmoji(JSON.parse(item.userid).name)
          return item
        })
        if(list&&list.length>0){
           list = list.map((item,index)=>{
            item.txt = item.name
            item.name = '店家'
            item.tx = '/img/mine/tx.jpg'
            return item
          })
          data = [...data,...list]
        }
        this.setData({
          [name]:data
        })
      }
    })
  },
  // 获取用户信息
  getUser(){
    const that = this;
    wx.getStorage({
      key: 'user',
      success (res) {
        that.getData(that.EmojiExchangeString(JSON.parse(res.data).name))
      }
    })
  },
  getData(username){
    wx.request({
      url: 'http://localhost:8888/getuser',
      data:{username},
      method:"GET",
      success:(res)=>{
        if(res.data.code == 200){
          let data = res.data.data[0]
        this.setData({
          name:this.StringExchangeEmoji(data.username),
          direction:data.direction,
          tx:data.usertx,
          focus:data.focus?JSON.parse(data.focus):[],
          fans:data.fans?JSON.parse(data.fans):[]
        })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUser()
    var time = 0;
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: time
    })
    wx.request({
      url: `http://localhost:8888/getcollection`,
      data:{name:this.EmojiExchangeString(this.data.name)},
      success:(res)=>{
        time = 2000
        console.log(res.data)
        let list = res.data.list;
        let data = res.data.data?res.data.data:[];
        data = data.map((item,index)=>{
          item.watch = item.check
          item.img = JSON.parse(item.img)[0].url
          item.tx = JSON.parse(item.userid).tx
          item.name = this.StringExchangeEmoji(JSON.parse(item.userid).name)
          return item
        })
        if(list&&list.length>0){
           list = list.map((item,index)=>{
            item.txt = item.name
            item.name = '店家'
            item.tx = '/img/mine/tx.jpg'
            return item
          })
          data = [...data,...list]
        }
        this.setData({
          collection:data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let _this = this;
    wx.stopPullDownRefresh({
      success:()=>{
        _this.getUser()
      }
  })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})